﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using MandalaySoft.Common;
using System.Data.SqlClient;
using System.Data;

namespace SIMS.BL
{
    public class GradeScaleDetailDB
    {
        MyDatabase _db = new MyDatabase();

        public DataTable GetGradeScaleDetailList(int GradeScaleID, out MyError error)
        {
            DataTable _dt = new DataTable("tblGradeScale");
            SortedList _paramList = new SortedList();
            _paramList.Add("@GradeScaleID", GradeScaleID);

            _dt = _db.GetDataTable("sprocGradeScaleDetailSelectList", CommandType.StoredProcedure, _paramList, out error);
            return _dt;
        }

        public int Delete(int GradeScaleDetailID, out MyError error)
        {
            SortedList _paramList = new SortedList();
            _paramList.Add("@GradeScaleDetailID", GradeScaleDetailID);

            return _db.ExecuteNonSql("sprocGradeScaleDetailDeleteSingleItem", CommandType.StoredProcedure, _paramList, out error);
        }

        public GradeScaleDetail GetItem(int GradeScaleDetailID)
        {
            GradeScaleDetail item = new GradeScaleDetail();

            SortedList _paraList = new SortedList();
            _paraList.Add("@GradeScaleDetailID", GradeScaleDetailID);

            MyError _error = new MyError();
            SqlDataReader _reader = _db.GetSqlDataReader("sprocGradeScaleDetailSelectSingleItem", System.Data.CommandType.StoredProcedure, _paraList, out _error);
            item.Error = _error;

            if (_error.ErrorNo == 0 && _reader.Read())
            {
                item = FillData(_reader, item);
            }

            return item;
        }

        public GradeScaleDetail Save(GradeScaleDetail item)
        {
            try
            {
                SortedList _paraList = new SortedList();
                _paraList.Add("@GradeScaleDetailID", item.GradeScaleDetailID);
                _paraList.Add("@GradeScaleID", item.GradeScaleID);
                
                _paraList.Add("@Title", item.Title);
                _paraList.Add("@BreakOff", item.BreakOff);
                _paraList.Add("@WeightedGPA", item.WeightedGPA);
                _paraList.Add("@UnWeightedGPA", item.UnWeightedGPA);
                _paraList.Add("@Comment", item.Comment);
                _paraList.Add("@SortOrder", item.SortOrder);
                _paraList.Add("@IsActive", item.IsActive);
                _paraList.Add("@CreatedBy", item.CreatedBy);
                _paraList.Add("@ModifiedBy", item.ModifiedBy);

                MyError _error = new MyError();
                Int64 _retValue = _db.Execute("sprocGradeScaleDetailInsertUpdateSingleItem", System.Data.CommandType.StoredProcedure, _paraList, out _error);
                item.Error = _error;

                if (_retValue > 0)
                {
                    item.GradeScaleID = Convert.ToInt32(_retValue);
                    item.Error.ErrorMessage = "Saved successfully!";
                }
                else
                {
                    if (_error.ErrorNo == 0)
                    {
                        item.Error.ErrorNo = 10002;
                        item.Error.ErrorLocation = "GradeScaleDetail";
                        item.Error.ErrorMessage = "No row affected.";
                    }
                }

            }
            catch (Exception Ex)
            {
                item.Error.ErrorNo = 10001;
                item.Error.ErrorLocation = "GradeScaleDetail";
                item.Error.ErrorMessage = Ex.Message;
                item.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "GradeScaleDetail");
            }

            return item;
        }

        private GradeScaleDetail FillData(IDataReader aRow, GradeScaleDetail item)
        {
            try
            {
                item = new GradeScaleDetail();
                item.GradeScaleDetailID = aRow.GetInt32(aRow.GetOrdinal("GradeScaleID"));
                item.GradeScaleID = aRow.GetInt32(aRow.GetOrdinal("GradeScaleID"));
                
                item.Title = aRow.GetString(aRow.GetOrdinal("Title"));
                item.BreakOff = aRow.GetDecimal(aRow.GetOrdinal("BreakOff"));
                item.WeightedGPA = aRow.GetDecimal(aRow.GetOrdinal("WeightedGPA"));
                item.UnWeightedGPA = aRow.GetDecimal(aRow.GetOrdinal("UnWeightedGPA"));
                item.Comment = aRow.GetString(aRow.GetOrdinal("Comment"));
                item.SortOrder = aRow.GetInt16(aRow.GetOrdinal("SortOrder"));
                item.IsActive = aRow.GetBoolean(aRow.GetOrdinal("IsActive"));
                item.CreatedBy = aRow.GetInt32(aRow.GetOrdinal("CreatedBy"));
                if (_db.ColumnExists(aRow, "CreatedByName") && aRow["CreatedByName"] != DBNull.Value)
                {
                    item.CreatedByName = aRow.GetString(aRow.GetOrdinal("CreatedByName"));
                }
                item.CreatedDate = aRow.GetDateTime(aRow.GetOrdinal("CreatedDate"));
                item.ModifiedBy = aRow.GetInt32(aRow.GetOrdinal("ModifiedBy"));
                if (_db.ColumnExists(aRow, "ModifiedByName") && aRow["ModifiedByName"] != DBNull.Value)
                {
                    item.ModifiedByName = aRow.GetString(aRow.GetOrdinal("ModifiedByName"));
                }
                item.ModifiedDate = aRow.GetDateTime(aRow.GetOrdinal("ModifiedDate"));
            }
            catch (Exception Ex)
            {
                item.Error.ErrorNo = 10003;
                item.Error.ErrorLocation = "GradeScaleDetail";
                item.Error.ErrorMessage = Ex.Message;
                item.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "GradeScaleDetail");
            }

            return item;
        }
    }
}
